﻿using System.Text;
using System.Windows.Forms;
using CommLib;
using InTheHand.Net.Sockets;
using System;

namespace Win32App
{
    public partial class MainForm : Form, IBtCallback
    {
        private delegate void DelOnMessageAlive();
        private DelOnMessageAlive mDelOnMessageAlive;
        private delegate void DelOnMessageBattery(int percent);
        private DelOnMessageBattery mDelOnMessageBattery;
        private delegate void DelOnMessageSignal(int percent);
        private DelOnMessageSignal mDelOnMessageSignal;
        private delegate void DelOnMessageContacts(Contact[] c);
        private DelOnMessageContacts mDelOnMessageContacts;
        private delegate void DelOnMessageConversations(Conversation[] c);
        private DelOnMessageConversations mDelOnMessageConversations;
        private delegate void DelOnMessageSms(Sms s);
        private DelOnMessageSms mDelOnMessageSms;
        private delegate void DelOnMessageSmsList(int threadId, Sms[] s);
        private DelOnMessageSmsList mDelOnMessageSmsList;
        
        private BtConnection mBtConn;

        public MainForm()
        {
            InitializeComponent();

            mDelOnMessageAlive = new DelOnMessageAlive(ActionOnMessageAlive);
            mDelOnMessageBattery = new DelOnMessageBattery(ActionOnMessageBattery);
            mDelOnMessageSignal = new DelOnMessageSignal(ActionOnMessageSignal);
            mDelOnMessageContacts = new DelOnMessageContacts(ActionOnMessageContacts);
            mDelOnMessageConversations = new DelOnMessageConversations(ActionOnMessageConversations);
            mDelOnMessageSms = new DelOnMessageSms(ActionOnMessageSms);
            mDelOnMessageSmsList = new DelOnMessageSmsList(ActionOnMessageSmsList);
        }

        private void MainForm_Load(object sender, System.EventArgs e)
        {
            mBtConn = new BtConnection(this);
            BluetoothDeviceInfo[] devs = mBtConn.PairedDevices;
            mCboPairedDevices.DisplayMember = "DeviceName";
            mCboPairedDevices.ValueMember = "DeviceAddress";
            mCboPairedDevices.DataSource = devs;
        }

        public void onNoDevice()
        {
            MessageBox.Show("Kein Bluetooth Adapter");
        }

        public void onDisabled()
        {
            MessageBox.Show("Bluetooth Adapter ist deaktiviert");
        }

        public void onNoPairedDevices()
        {
            MessageBox.Show("Keine gepaarten Geräte");
        }

        public void onMessageAlive()
        {
            Invoke(mDelOnMessageAlive);
        }

        private void ActionOnMessageAlive()
        {
            mRdoAlive.Checked = !mRdoAlive.Checked;
        }

        public void onMessageBattery(int percent)
        {
            Invoke(mDelOnMessageBattery, percent);
        }

        private void ActionOnMessageBattery(int percent)
        {
            mPrgBattery.Value = percent;
        }

        public void onMessageSignal(int percent)
        {
            Invoke(mDelOnMessageSignal, percent);
        }

        private void ActionOnMessageSignal(int percent)
        {
            mPrgSignal.Value = percent;
        }

        public void onMessageContacts(Contact[] c)
        {
            Invoke(mDelOnMessageContacts, new object[]{c});
        }

        private void ActionOnMessageContacts(Contact[] c)
        {
            mLstContacts.DisplayMember = "Name";
            mLstContacts.DataSource = c;
        }

        public void onMessageConversations(Conversation[] c)
        {
            Invoke(mDelOnMessageConversations, new object[] { c });
        }

        private void ActionOnMessageConversations(Conversation[] c)
        {
            mLstConversations.DataSource = c;
        }

        public void onMessageSms(Sms s)
        {
            Invoke(mDelOnMessageSms, s);
        }

        private void ActionOnMessageSms(Sms s)
        {
            // TODO
        }

        public void onMessageConversation(Conversation c)
        {
            // TODO
        }

        public void onMessageSmsList(int threadId, Sms[] s)
        {
            Invoke(mDelOnMessageSmsList, new object[] { threadId, s });
        }

        private void ActionOnMessageSmsList(int threadId, Sms[] s)
        {
            StringBuilder b = new StringBuilder();
            b.Append("ThreadID: ");
            b.AppendLine(threadId.ToString());

            foreach (Sms sms in s)
            {
                b.AppendLine(sms.ToString());
                b.AppendLine();
            }

            mTxtConversation.Text = b.ToString();
        }

        public void onConnectionClosed()
        {
            MessageBox.Show("Verbindung verloren");
        }

        public void onReconnected()
        {
            MessageBox.Show("Reconnected");
        }

        private void mBtnConnect_Click(object sender, System.EventArgs e)
        {
            if (mCboPairedDevices.SelectedItem != null)
            {
                BluetoothDeviceInfo dev = mCboPairedDevices.SelectedItem as BluetoothDeviceInfo;
                bool connected = mBtConn.connect(dev.DeviceAddress);

                mBtnConnect.Enabled = !connected;
                mBtnDisconnect.Enabled = connected;
            }
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            mBtConn.disconnect();
        }

        private void mBtnDisconnect_Click(object sender, System.EventArgs e)
        {
            mBtConn.disconnect();
            mBtnConnect.Enabled = true;
            mBtnDisconnect.Enabled = false;
        }

        private void mLstConversations_DoubleClick(object sender, System.EventArgs e)
        {
            Conversation c = mLstConversations.SelectedValue as Conversation;
            mBtConn.requestConversationContent(c.getThreadId());
        }


        #region IBtCallback Member

        public void onMessageCalendar(int calendarId, CalendarEntry[] c)
        {
            throw new System.NotImplementedException();
        }

        public void onMessageCalendars(AndroidCalendar[] c)
        {
            throw new System.NotImplementedException();
        }

        public void onMessageSmsSent(bool success)
        {
            throw new System.NotImplementedException();
        }

        public void onMessageSmsDelivered(bool success)
        {
            throw new System.NotImplementedException();
        }

        public void onMessageCallLog(CallLogType type, CallLogEntry[] c)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
